﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

using RemotingFramework = DreamCube.Framework.Utilities.Remoting;

namespace RemotingActiveX
{
    [Guid("FE14373E-8488-4C02-A985-B6615CB49631"), ComVisible(true), ComSourceInterfaces(typeof(ActiveXDemo_Event))]
    public class ActiveXDemo
    {
        public delegate void HelloWorldHandler();
        public delegate void ShowMessageHandler(String data);

        public event HelloWorldHandler HelloWorld;
        public event ShowMessageHandler ShowMessage;

        /// <summary>
        /// 提示helloworld信息
        /// </summary>
        public void ShowHelloWorld()
        {
            MessageBox.Show("Hello World");
        }

        public void ConnectServer()
        {
            RemotingFramework.Client.Instance.InitialClient();
            Remoting.ActiveXDemo demo = RemotingFramework.Server.Instance.CreateRemotingObj<Remoting.ActiveXDemo>(9002, "tcp", "127.0.0.1", false);
            RemotingFramework.EventWrapper<String> eventWrapper = new RemotingFramework.EventWrapper<String>();
            eventWrapper.TargetEvent += new Action<String>(eventWrapper_TargetEvent);
            RemotingFramework.Server.Instance.RegisterRemotingObjByInstance(demo);
            demo.ShowMessage += eventWrapper.FireEvent;
        }

        void eventWrapper_TargetEvent(string obj)
        {
            MessageBox.Show(obj);
        }

        void demo_ShowMessage(String obj)
        {
        }
    }
}